package com.example.activiti6_demo.demo2;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * 判断节点，排他网关
 */
@SpringBootTest
public class E_ExclusiveGateway {

    /**
     * 仓储服务，用于管理流程仓库，例如：部署，删除，读取流程资源
     * 可以用来部署我们的流程图，还可以创建我们的流程部署查询对象，用于查询刚刚部署的流程列表，便于我们管理流程
     */
    @Resource
    private RepositoryService repositoryService;

    /**
     * 运行时服务，可以处理所有正在运行状态的流程实例，任务等
     * 主要用来开启流程实例，一个流程实例对应多个任务，也就是多个流程节点
     */
    @Resource
    private RuntimeService runtimeService;

    /**
     * 排他网关测试，基于receiveTask机器自动任务测试
     */
    @Test
    public void exclusiveGateway() throws Exception{
        repositoryService.createDeployment().addClasspathResource("processes/exclusiveGateway.bpmn").deploy();

        ProcessInstance instance = runtimeService.startProcessInstanceByKey("exclusiveGateway");
        String pid = instance.getId();
        System.out.println("流程实例id：" + pid);

        //判断是否在“汇总当日销售额ID”节点
        Execution e1 = runtimeService.createExecutionQuery().processInstanceId(pid).activityId("汇总当日销售额ID").singleResult();
        assertNotNull(e1);

        //业务处理获得结果
        Double money = 18888.88d;
        runtimeService.setVariable(e1.getId(), "money", money);
        runtimeService.trigger(e1.getId());

        Execution e2 = runtimeService.createExecutionQuery().processInstanceId(pid).activityId("给老板发短信ID").singleResult();
        if (null != e2) {
            System.out.println("当前节点是：" + e2.getActivityId());
            Double mon = runtimeService.getVariable(e2.getId(), "money", Double.class);
            System.out.println("短信：老板，今天赚了：" + mon + "元");
            runtimeService.trigger(e2.getId());
        }

        Execution e3 = runtimeService.createExecutionQuery().processInstanceId(pid).activityId("给老板发邮件ID").singleResult();
        if (null != e3) {
            System.out.println("当前节点是：" + e3.getActivityId());
            Double mon = runtimeService.getVariable(e3.getId(), "money", Double.class);
            System.out.println("邮件信：老板，今天大赚了：" + mon + "元");
            runtimeService.trigger(e3.getId());
        }

        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(pid).singleResult();
        Assertions.assertNull(processInstance);
        System.out.println("流程完成");
    }

    /**
     * 排他网关测试，基于receiveTask机器自动任务测试,添加默认出口
     * 修改xml的“判断销售额ID”节点，添加 default="默认出口ID" ，关联默认节点
     * <exclusiveGateway default="默认出口ID" gatewayDirection="Unspecified" id="判断销售额ID" name="判断销售额"/>
     */
    @Test
    public void exclusiveGateway2() throws Exception{
        repositoryService.createDeployment().addClasspathResource("processes/exclusiveGateway2.bpmn").deploy();

        ProcessInstance instance = runtimeService.startProcessInstanceByKey("exclusiveGateway2");
        String pid = instance.getId();
        System.out.println("流程实例id：" + pid);

        //判断是否在“汇总当日销售额ID”节点
        Execution e1 = runtimeService.createExecutionQuery().processInstanceId(pid).activityId("汇总当日销售额ID").singleResult();
        assertNotNull(e1);

        //业务处理获得结果
        Double money = 18888.88d;
        runtimeService.setVariable(e1.getId(), "money", money);
        runtimeService.trigger(e1.getId());

        Execution e2 = runtimeService.createExecutionQuery().processInstanceId(pid).activityId("给老板发短信ID").singleResult();
        if (null != e2) {
            System.out.println("当前节点是：" + e2.getActivityId());
            Double mon = runtimeService.getVariable(e2.getId(), "money", Double.class);
            System.out.println("短信：老板，今天赚了：" + mon + "元");
            runtimeService.trigger(e2.getId());
        }

        Execution e3 = runtimeService.createExecutionQuery().processInstanceId(pid).activityId("给老板发邮件ID").singleResult();
        if (null != e3) {
            System.out.println("当前节点是：" + e3.getActivityId());
            Double mon = runtimeService.getVariable(e3.getId(), "money", Double.class);
            System.out.println("邮件信：老板，今天大赚了：" + mon + "元");
            runtimeService.trigger(e3.getId());
        }

        Execution e4 = runtimeService.createExecutionQuery().processInstanceId(pid).activityId("默认出口ID").singleResult();
        if (null != e4) {
            System.out.println("当前节点是：" + e4.getActivityId());
            Double mon = runtimeService.getVariable(e4.getId(), "money", Double.class);
            System.out.println("默认：老板，今天小赚了：" + mon + "元");
            runtimeService.trigger(e4.getId());
        }

        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(pid).singleResult();
        Assertions.assertNull(processInstance);
        System.out.println("流程完成");
    }
}
